package com.jz.digital.login

import android.content.Context
import android.content.Intent
import android.view.View
import com.jz.digital.R
import com.jz.digital.base.BaseActivity
import com.jz.digital.login.set.LoginSetActivity
import com.jz.digital.main.MainActivity
import com.jz.digital.utils.SPHelper
import kotlinx.android.synthetic.main.activity_login.cb_remember_password
import kotlinx.android.synthetic.main.activity_login.image_set
import kotlinx.android.synthetic.main.activity_login.password_view
import kotlinx.android.synthetic.main.activity_login.phone_view
import kotlinx.android.synthetic.main.activity_login.image_login

/**
 * @author WangYong
 * @date 2024/11/18 18:49
 * @description:
 */
class LoginActivity : BaseActivity<LoginPresenter>(), ILoginView, View.OnClickListener {
    override fun getLayoutResId(): Int {
        return R.layout.activity_login
    }

    override fun initViews() {
        super.initViews()

    }

    override fun initData() {
        super.initData()
        phone_view.setText(SPHelper.getAccount())
        val isRememberPassword = SPHelper.isRememberPassword()
        cb_remember_password.isChecked = isRememberPassword
        if (isRememberPassword) {
            password_view.setPassword(SPHelper.getPassword())
        }
    }

    override fun initListening() {
        super.initListening()
        image_set.setOnClickListener(this)
        image_login.setOnClickListener(this)
    }

    companion object {
        @JvmStatic
        fun startActivity(context: Context) {
            context.startActivity(Intent(context, LoginActivity::class.java))
        }
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.image_set -> {
                //设置
                LoginSetActivity.startActivity(this)
            }

            R.id.image_login -> {
                //登陆
                val account = phone_view.phone.trim()
                val password = password_view.password.trim()
                if (account.isEmpty()) {
                    showToast(R.string.please_input_account)
                    return
                }
                if (password.isEmpty()) {
                    showToast(R.string.please_input_password)
                    return
                }
                if (SPHelper.getLocalLanAddress().isEmpty()) {
                    showToast(R.string.please_set_lan_address)
                    return
                }
                SPHelper.saveAccount(account)
                SPHelper.saveRememberPassword(cb_remember_password.isChecked)
                SPHelper.savePassword(if (cb_remember_password.isChecked) password else "")
                showLoadingDialog()
                mPresenter.login(account, password)
            }
        }
    }

    override fun onLoginSuccess() {
        startActivity(Intent(this, MainActivity::class.java))
        finishAfterTransition()
    }

    override fun onLoginFail(message: String) {
        dismissLoadingDialog(true)
        showToast(message)
    }
}